package dhm.com.hpx.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.ControllerListener;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.image.ImageInfo;

import java.util.ArrayList;
import java.util.List;

import dhm.com.hpx.R;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.entity.MainPosterBean;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class MainPosterAdapter extends RecyclerView.Adapter<MainPosterAdapter.ViewHolder> {

    private Context context;


    List<MainPosterBean.DataBean> list=new ArrayList<>();

    public MainPosterAdapter(Context context) {
        this.context = context;
    }


    public void setList(List<MainPosterBean.DataBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view= LayoutInflater.from(context).inflate(R.layout.recy_poster, null);
        viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {

        viewHolder.num.setText(list.get(i).getShareNum()+"");

        //获取图片真正的宽高
        Glide.with(context).asBitmap().load(Constant.PATH+list.get(i).getCoverImg()).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                imageShow(viewHolder.image,Constant.PATH+list.get(i).getCoverImg(),height);
            }
        });


        if (list.get(i).getIsElite()==1){
            viewHolder.like.setImageResource(R.mipmap.likes);
        }else {
            viewHolder.like.setImageResource(R.mipmap.liken);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onClick.item(i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public SimpleDraweeView image;
        public ImageView like;
        public TextView num;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.image);
            like=itemView.findViewById(R.id.like);
            num=itemView.findViewById(R.id.num);
        }
    }

    private OnClick onClick;
    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }
    public interface OnClick{
        void item(int posttion);
    }

    private OnIntentClick onIntentClick;
    public void setaddClick(OnIntentClick onClick) {
        this.onIntentClick = onClick;
    }
    public interface OnIntentClick{
        void item();
    }


    /**
     * 设置宽高的图片
     * @param image
     * @param imageURI
     * @param tempHeight
     */
    private void imageShow(final SimpleDraweeView image, String imageURI,final int tempHeight) {
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                .setOldController(image.getController())
                .setControllerListener(new ControllerListener<ImageInfo>() {
                    @Override
                    public void onSubmit(String id, Object callerContext) {

                    }

                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) image.getLayoutParams();
                        params.height = tempHeight;
                        image.setLayoutParams(params);
                    }

                    @Override
                    public void onIntermediateImageSet(String id, ImageInfo imageInfo) {

                    }

                    @Override
                    public void onIntermediateImageFailed(String id, Throwable throwable) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {

                    }

                    @Override
                    public void onRelease(String id) {

                    }
                })
                .setUri(Uri.parse(imageURI))
                .build();
        image.setController(controller);
    }


}

